#!/usr/bin/env groovy

/**
 # 基于k8s集群发布
 # Author yousong.xiang
 # Date 2025.1.6
 # v1.0.1
 # helm发布命令以字符串的形式传入

 **/

/**
 # 参数信息：
 @Library('ops-pipline-library')_

  // @Library('xiangys0134_test')_
  def map = [:]
  // 自定义构建运行node节点环境,根据实际情况进行调整
  map.put('JENKINS_NODE','brokersview')
  // 应用的名称，最好与helm定义的应用一致
  map.put('APP_NAME','brokersview-web')
  map.put('DOCKER_REGISTRY','https://registry.cn-shenzhen.aliyuncs.com')
  map.put('DOCKER_IMG','registry.cn-shenzhen.aliyuncs.com/stl-test/brokersview')
  map.put('DOCKER_REGISTRY_KEY','fb805ed9-7e7e-4c79-a9e4-325d5afacc7a')
  map.put('GIT_URL','ssh://git@gitwbp5.tostar.top/Bailun/brokers-view-web.git')
  map.put('GIT_KEY','92b42e30-38f2-442d-a2c2-9763fb14d6bf')
  map.put('DEPLOY_JENKINS_JOB','yunwei_update_test')
  map.put('DEPLOY_MAP','release_dev:yunwei_update_test,release_test:v2')


  deploy_front_npm_base(map)
  **/

def call(Map map) {
    pipeline {
        agent {
            node {
                label map.JENKINS_NODE
            }
        }

        environment {
            // sDate=time()
            app_name="${map.APP_NAME}"
            // dockerfile="Dockerfile.${PROFILE}"
            dockerfile="${map.DOCKERFILE_NAME}"
            dockerdir='./'
            // dockerregistry='https://553972031073.dkr.ecr.ap-east-1.amazonaws.com'
            dockerregistry="${map.DOCKER_REGISTRY}"
            docker_image="${map.DOCKER_IMG}"
            startDate=timeForm()

            data_agent=dataAgent()
            envtag=imageTag()

            docker_image_tag="${env.app_name}-${env.envtag}"
            docker_key="${map.DOCKER_REGISTRY_KEY}"
            git_url="${map.GIT_URL}"
            git_key="${map.GIT_KEY}"
            dev_tag=varEnv.branchTag(BRANCH)
            project='code_directory'
            deploy_map="${map.DEPLOY_MAP}"
            deploy_update_job="${map.DEPLOY_JENKINS_JOB}"
            jobs = varEnv.updateJobList(JOB_UPDATE_LIST,deploy_map)
            build_cmd="${map.BUILD_CMD}"
            build_agrs="${map.DOCKER_BUILD_ARGS}"
            build_dir_check=varEnv.buildDir("${map.BUILD_DIR}")
            docker_build_dir="${env.WORKSPACE}/${env.project}/${env.build_dir_check}"
        }
        stages {
            stage('删除工作空间') {
                steps {
                    script{
                        deleteDir()
                    }
                }
            }
            stage('拉取git仓库') {
                steps {
                    sshagent(credentials: [git_key], ignoreMissing: true) {
                        dir("${env.WORKSPACE}") {
                            sh '''
                    git clone --depth 1 --branch=${dev_tag} ${git_url} ${WORKSPACE}/${project}
                 '''
                        }
                    }
                }
            }
            stage('代码编译') {
                steps {
                    script {
                        dir("${env.docker_build_dir}") {
                            sh'''
                #执行shell命令
                if [ "${build_cmd}" != "null" ]; then
                  echo "running exec: ${build_cmd}"
                  sh -c "${build_cmd}"
                fi

              '''
                        }
                    }
                }
            }
            stage('build构建镜像') {
                steps {
                    dir("${env.docker_build_dir}"){
                        script {
                            try {
                                withDockerRegistry(credentialsId: docker_key, url: dockerregistry) {
                                    def customImage = docker.build("${docker_image}:${docker_image_tag}", "${build_agrs} ${dockerfile} ${dockerdir}")
                                    customImage.push()
                                }
                            }
                            catch(exc){
                                currentBuild.result='FAILURE'
                                echo'构建失败'
                                sh'exit 4'
                            }
                        }
                    }
                }
            }

            stage('关联发布') {
                when {
                    allOf{
                        expression {
                            return env.jobs != 'null';
                        }
                    }
                }
                steps {
                    script{
                        for(item in jobs.tokenize(',')){
                            build job: item, parameters: [
                                    string(name: 'release_tag', value: env.docker_image_tag)
                            ]
                        }
                    }
                }
            }
        }
    }

}
